package com.liucxu.zimuzu.Presenter.Impl;

import com.liucxu.zimuzu.Model.Bean.ResourceDetailsInfoBean;
import com.liucxu.zimuzu.Model.Bean.UpdateBean;
import com.liucxu.zimuzu.Model.Bean.UpdateEntity;
import com.liucxu.zimuzu.Model.IResourceInfoModel;
import com.liucxu.zimuzu.Model.IUpdateModel;
import com.liucxu.zimuzu.Model.Impl.ResourceInfoImpl;
import com.liucxu.zimuzu.Model.Impl.UpdateModelImpl;
import com.liucxu.zimuzu.Presenter.IUpdatePresenter;
import com.liucxu.zimuzu.Utils.TransformUtils;
import com.liucxu.zimuzu.View.IView.IUpdateView;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * 今日更新Presenter实现类
 * Created by liukanshan on 2016/12/8.
 */

public class UpdatePresenterImpl implements IUpdatePresenter {

    private IUpdateView mUpdateView;
    private IUpdateModel mUpdateModel;
    private IResourceInfoModel mIResourceInfoModel;

    public UpdatePresenterImpl(IUpdateView updateView) {
        mUpdateView = updateView;
        mUpdateModel = new UpdateModelImpl();
        mIResourceInfoModel = new ResourceInfoImpl();
    }


    @Override
    public void getUpdateToday() {
        mUpdateView.showLoading();
        final List<String> resIds = new ArrayList<>();
        mUpdateModel.getUpdateToday().flatMap(new Func1<UpdateBean, Observable<UpdateEntity>>() {
            @Override
            public Observable<UpdateEntity> call(UpdateBean updateBean) {
                return Observable.from(updateBean.getData());
            }
        }).filter(new Func1<UpdateEntity, Boolean>() {
            @Override
            public Boolean call(UpdateEntity updateEntity) {
                if (resIds.contains(updateEntity.getResourceid())) {
                    return false;
                } else {
                    resIds.add(updateEntity.getResourceid());
                    return true;
                }
            }
        }).flatMap(new Func1<UpdateEntity, Observable<ResourceDetailsInfoBean>>() {
            @Override
            public Observable<ResourceDetailsInfoBean> call(final UpdateEntity updateEntity) {
                return mIResourceInfoModel.getResourceInfo(updateEntity.getResourceid()).flatMap(new Func1<ResourceDetailsInfoBean, Observable<ResourceDetailsInfoBean>>() {
                    @Override
                    public Observable<ResourceDetailsInfoBean> call(ResourceDetailsInfoBean resourceDetailsInfoBean) {
                        resourceDetailsInfoBean.getData().setWays(updateEntity.getWays());
                        return Observable.just(resourceDetailsInfoBean);
                    }
                });
            }
        })
                .toList()
                .compose(TransformUtils.<List<ResourceDetailsInfoBean>>defaultSchedulers()).subscribe(new Subscriber<List<ResourceDetailsInfoBean>>() {
            @Override
            public void onCompleted() {
                mUpdateView.hideLoading();
            }

            @Override
            public void onError(Throwable e) {
                mUpdateView.hideLoading();
                mUpdateView.showError(e.getMessage());
            }

            @Override
            public void onNext(List<ResourceDetailsInfoBean> resourceDetailsInfoBeen) {
                mUpdateView.hideLoading();
                mUpdateView.setUpdates(resourceDetailsInfoBeen);
            }
        });
    }
}
